package com.sh.data.engine.web.webapi.controller.integration;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.integration.*;
import com.sh.data.engine.application.model.dto.workspace.UserResponse;
import com.sh.data.engine.application.service.integration.OfflineSyncOperationApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.integration.offline.service.OfflineSyncRunningRecordService;
import com.sh.data.engine.domain.integration.offline.service.OfflineSyncService;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@RestController(value = "OfflineSyncOperatorController")
@RequestMapping("/integration/offline/operation")
@Tag(
    name = "数据集成-任务运维-v2",
    description = "数据集成-任务运维-v2 API")
public class OfflineSyncOperationController {

    @Autowired
    private OfflineSyncOperationApplicationService offlineSyncOperationApplicationService;

    @Autowired
    private OfflineSyncRunningRecordService offlineSyncRunningRecordService;

    @Autowired
    private OfflineSyncService offlineSyncService;

    @Operation(summary = "获取任务的统计数-head")
    @GetMapping("/summary")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 0)
    public RestResponseEntity<OfflineSyncOperationSummaryResponse> getOfflineSyncSummary() {
        var result = offlineSyncOperationApplicationService.getOfflineSyncOperationSummary();
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "今日任务完成情况")
    @GetMapping("/finished/today")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 0)
    public RestResponseEntity<OfflineSyncOperationFinishedResponse> getTodayFinishedResponse() {
        var result = offlineSyncOperationApplicationService.getTodayFinished();
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "失败任务top 10")
    @GetMapping("/failedTask/top")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 0)
    public RestResponseEntity<List<OfflineSyncOperationFailedTopResponse>> getFailedTaskTopResponse(
        @Parameter(description = "0-三天 1-一周 2- 一个月", required = false)
        @RequestParam(value = "timeType", required = true, defaultValue = "1")
        Integer timeType) {
        var result = offlineSyncOperationApplicationService.getFailedTaskTop(timeType);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "运行时长排行榜")
    @GetMapping("/runningTime/top")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 0)
    public RestResponseEntity<List<OfflineSyncOperationRunningTopResponse>> getRunningTimeTopResponse(
        @RequestParam(value = "runDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd")
        LocalDate runDate) {
        var result = offlineSyncOperationApplicationService.getRunningTimeTop(runDate);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "任务监控异常情况")
    @GetMapping("/taskMonitor/list")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 0)
    public RestResponseEntity<OfflineSyncOperationTaskMonitorResponse> getTaskMonitorListResponse() {
        var result = offlineSyncOperationApplicationService.getTaskMonitorError();
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "异常任务监控")
    @GetMapping("/taskMonitor/error")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 0)
    public RestResponseEntity<PagerResponse<OfflineSyncOperationTaskMonitorErrorResponse>>
    getTaskMonitorErrorTaskResponse(
        @Parameter(description = "pageNum")
        @RequestParam(value = "pageNum", required = false, defaultValue = "1")
        Integer pageNum,
        @Parameter(description = "pageSize")
        @RequestParam(value = "pageSize", required = false, defaultValue = "10")
        Integer pageSize,
        @Parameter(description = "1-三天 2-一周 3- 一个月", required = false)
        @RequestParam(value = "timeCase", required = true, defaultValue = "1")
        Integer timeCase) {
        var result = offlineSyncOperationApplicationService.getTaskMonitorErrorTask(pageNum, pageSize, timeCase);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 0)
    @GetMapping("/task/history")
    @Operation(summary = "历史任务")
    public RestResponseEntity<PagerResponse<OfflineSyncRunningRecordResponse>> historyTask(
        @Parameter(description = "当前页", schema = @Schema(defaultValue = "1"))
        @RequestParam(required = false, defaultValue = "1")
        Integer pageNum,
        @Parameter(description = "每页大小", schema = @Schema(defaultValue = "10"))
        @RequestParam(required = false, defaultValue = "10")
        Integer pageSize,
        @Parameter(description = "任务id") @RequestParam(required = false) Long taskId,
        @Parameter(description = "任务状态") @RequestParam(required = false) String taskStatus,
        @Parameter(description = "任务类型 0-周期 1-手动") @RequestParam(required = false) String executeType,
        @Parameter(description = "用户Id") @RequestParam(required = false) String creatorId,
        @Parameter(description = "开始时间")
        @RequestParam(required = false)
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        Date startTime,
        @Parameter(description = "结束时间")
        @RequestParam(required = false)
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        Date endTime,
        @Parameter(description = "用户") @RequestParam(required = false) String userId,
        @Parameter(description = "排序类型") @RequestParam(required = false) String order,
        @Parameter(description = "排序字段") @RequestParam(required = false) String orderField,
        @Parameter(description = "任务名称") @RequestParam(required = false) String nodeName,
        @Parameter(description = "忽略") @RequestParam(required = false) Integer ignored) {
        var result = offlineSyncOperationApplicationService.historyTask(
            pageNum, pageSize, taskId, taskStatus, executeType, creatorId, startTime, endTime, userId, order, orderField, nodeName, ignored);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 1)
    @Operation(summary = "终止运行任务", description = "终止运行任务")
//    @ApiImplicitParams({
//        @ApiImplicitParam(
//            paramType = "header",
//            dataType = "String",
//            name = Constants.HEADER_PROJECT_ID,
//            value = "项目id标识",
//            required = true),
//    })
    @PostMapping("/stop")
    @LogExecution(operation = EngineOperation.STOP_TASK, business = Business.离线同步任务)
//    @Log(
//        business = Business.离线同步任务,
//        operation = Operation.STOP_TASK)
    public RestResponseEntity<Boolean> taskStop(
        @Parameter(description = "job id 以逗号分开", required = true) @RequestParam("logIds") String logIds) {
        var result = offlineSyncOperationApplicationService.stopTasks(logIds);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.RWGLOFFLINE, readWriteStatus = 0)
    @Operation(summary = "运行离线同步任务")
    @GetMapping("/run")
    @LogExecution(operation = EngineOperation.START_TASK, business = Business.离线同步任务)
//    @Log(
//        business = Business.离线同步任务,
//        operation = Operation.START_TASK)
    public RestResponseEntity<String> runTask(
        @Parameter(description = "离线同步任务id") @RequestParam Long taskId,
        @Parameter(description = "运行方式 1-试运行 0-重跑") @RequestParam Integer runType) {
        return RestResponseEntity.success(offlineSyncOperationApplicationService.run(taskId, runType));
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 0)
    @Operation(summary = "获取日志信息", description = "获取日志信息")
//    @ApiImplicitParams({
//        @ApiImplicitParam(
//            paramType = "header",
//            dataType = "String",
//            name = Constants.HEADER_PROJECT_ID,
//            value = "项目id标识",
//            required = true),
//    })
    @GetMapping("/log")
    public RestResponseEntity<OfflineSyncLogResponse> getOfflineSyncLog(
        @Parameter(description = "离线同步任务LogId") @RequestParam Long logId,
        @Parameter(description = "偏移量", required = true) @RequestParam("offset") Integer offset) {
        var result = offlineSyncOperationApplicationService.getOfflineSyncLog(logId, offset);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "查询用户信息列表")
    @GetMapping("/userList")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 0)
    public RestResponseEntity<List<UserResponse>> getUserList() {
        var result = offlineSyncOperationApplicationService.getUserList();
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "忽略任务")
    @GetMapping("/ignore")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYFOFFLINE, readWriteStatus = 1)
    public RestResponseEntity<Boolean> ignoreOfflineSync(String ids) {
        var result = offlineSyncOperationApplicationService.ignoreTasks(ids);
        return RestResponseEntity.success(result);
    }

}
